////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek, 1999-2010.
// -------------------------------------------------------------------------
//  File name:	AssetBrowserDialog.h
//  Version:	v1.00
//  Created:	30/07/2008 by Paulo Zaffari
//  Description:	MFC class implementing the Asset browser dialog
// -------------------------------------------------------------------------  
//  History:
//		30/07/2008  17:24 - Paulo Zaffari - created
//		12/03/2010  12:48 - Nicusor Nedelcu - refactored
//
//////////////////////////////////////////////////////////////////////////// 

#ifndef __AssetBrowserDialog_h__
#define __AssetBrowserDialog_h__
#pragma once

#include <map>
#include <vector>
#include "IEditor.h"
#include "Dialogs\BaseFrameWnd.h"
#include "../Controls/AssetViewer.h"
#include "Visual Budget System/VisualBudgetSystemSettings.h"
#include "Asset Browser/AssetBrowserSearchDlg.h"
#include "Asset Browser/AssetBrowserFilterPresetsDlg.h"
#include "Asset Browser/AssetBrowserPreviewDlg.h"
#include "Controls/DynamicRibbonBar.h"

#define ASSET_BROWSER_NAME "Asset Browser"
#define ASSET_BROWSER_VER "1.00"

namespace AssetBrowser
{
	const UINT kMaxDatabaseCount = 100;
};

#define ID_ASSET_BROWSER_DATABASE_CHECKBOX_ITEM_START ( WM_USER )
#define ID_ASSET_BROWSER_DATABASE_CHECKBOX_ITEM_END ( ID_ASSET_BROWSER_DATABASE_CHECKBOX_ITEM_START + AssetBrowser::kMaxDatabaseCount )
#define ID_ASSET_BROWSER_TIMER_REFRESH_OPERATION_INFO 1
#define ID_ASSET_BROWSER_TIMER_REPORT_CONTROL 2
#define ID_ASSET_BROWSER_TIMER_CHECK_DB_INFO_LOADED 3
#define ID_ASSET_BROWSER_TIMER_UPDATE_CACHING_PROGRESS 4

//! This class is the actual asset browser list view
class CAssetBrowserReportControl : public CXTPReportControl
{
	public:

		CAssetBrowserReportControl();

		void OnSelectionChanged();
		void ClearVisibleItems();
		void AddVisibleItem(IAssetDisplay *pAsset);

		CAssetBrowserDialog* m_pAssetBrowserDlg;

	protected:

		DECLARE_MESSAGE_MAP()
		afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
		afx_msg void OnTimer(UINT_PTR nIDEvent);

		bool CacheFieldsInfoForVisibleAsset();

		TAssetItems m_cVisibleItems;
};

// The comment: "/*public CDialog*/" is a helper in case we want to add more stuff using 
// resource editor and MFC.
class CAssetBrowserDialog : /*public CDialog,*/ public CBaseFrameWnd ,/*public IAssetViewerStatusDisplay,*/public IEditorNotifyListener, public IAssetViewerObserver
{
	private:

		DECLARE_DYNCREATE( CAssetBrowserDialog )

	public:

		friend class CAssetBrowserSearchDlg;

		CAssetBrowserDialog( /*CWnd* pParent = NULL*/ );   // standard constructor
		virtual ~CAssetBrowserDialog();
		static void RegisterViewClass();

		// Dialog Data
		enum { IDD = IDD_TEXTURE_BROWSER_DIALOG };

		//////////////////////////////////////////////////////////////////////////
		// IEditorNotifyListener
		//////////////////////////////////////////////////////////////////////////
		void OnEditorNotifyEvent( EEditorNotifyEvent event );

		static CAssetBrowserDialog*				GetCurrentInstance();
		void 															GetSelectedItems( TAssetItems& rcpoSelectedItemArray );
		void															CreateRibbonBar();
		void															UpdateAssetDatabaseForVisualBudgetSystem( const std::vector<AABB>& AABBs );
		CAssetViewer&											GetAssetViewer();
		void															StartCacheAssetInfo();
		void															StopCacheAssetInfo();

	protected:

		//////////////////////////////////////////////////////////////////////////
		// IAssetViewerObserver
		//////////////////////////////////////////////////////////////////////////

		void OnChangeStatusBarInfo( UINT nSelectedItems, UINT nVisibleItems, UINT nTotalItems );
		void OnSelectionChanged();
		void OnChangedPreviewedAsset( IAssetDisplay* pAsset );

		virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
		DECLARE_MESSAGE_MAP()

	public:

		void					CreateAssetViewer();
		void					CreateDatabases();
		void					FreeDatabases();
		IAssetDisplayDatabase* GetDatabaseByName( const char* pName );
		void					ShowOnlyCheckedDatabases();
		void					SortAssets( const char* pFieldname, bool bDescending = false );
		void					ApplyFilters( IAssetDisplayDatabase::TAssetFieldFiltersMap& rFilterFields );
		void					ClearFilters();
		void					ResetAssetListView();
		void					ResizeSplitterPanes();
		void					ActivateFilterPreset( const char* pPresetName );
		void					ApplyAllFiltering();

		//
		// Commands
		//
		static void		Command_ShowViewportSelection();

		//
		// Message handlers
		//

		BOOL					OnInitDialog();
		BOOL					PreTranslateMessage(MSG* pMsg);
		void					PostNcDestroy();
		afx_msg	void	OnDestroy();
		afx_msg void	OnClose();
		afx_msg void	OnTimer(UINT_PTR nIDEvent);
		afx_msg void	OnSize(UINT nType, int cx, int cy);
		afx_msg void	OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
						void	UpdateRibbonButtons();
						void	UpdateFilterPresetsList();
		afx_msg void	OnAssetBrowserDatabase(UINT id);
		afx_msg void	OnUpdateAssetBrowserDatabase(CCmdUI* pCmdUI);
		afx_msg void	OnAssetBrowserDatabaseCheckAll();
		afx_msg void	OnAssetBrowserDatabaseUncheckAll();
		afx_msg void	OnAssetBrowserDatabaseRefresh();
		afx_msg void	OnAssetBrowserEditFilterPresets();
		afx_msg void	OnAssetBrowserChangeFilterPresets( NMHDR* pNMHDR, LRESULT* pResult );
		afx_msg void	OnAssetBrowserChangeThumbSize( NMHDR* pNMHDR, LRESULT* pResult );
		afx_msg void	OnUpdateAssetBrowserCtrl( CCmdUI* pCmdUI );
		afx_msg void	OnAssetBrowserRefreshUsedInLevel();
		afx_msg void	OnUpdateAssetBrowserUsedInLevel( CCmdUI* pUI );
		afx_msg void	OnUpdateAssetBrowserShowFavourites( CCmdUI* pUI );
		afx_msg void	OnUpdateAssetBrowserHideLods( CCmdUI* pUI );
		afx_msg void	OnUpdateAssetBrowserAutoChangeViewportSelection( CCmdUI* pUI );
		afx_msg void	OnUpdateAssetBrowserAutoFilterFromViewportSelection( CCmdUI* pUI );
		afx_msg void	OnAssetBrowserShowUsedInLevel();
		afx_msg void	OnAssetBrowserShowFavourites();
		afx_msg void	OnAssetBrowserHideLods();
		afx_msg	void	OnAssetBrowserChangedEditTags();
		afx_msg void	OnAssetBrowserRefreshLevelStats();
		afx_msg void	OnAssetBrowserFromViewportSelection();
		afx_msg void	OnAssetBrowserSelectInViewport();
		afx_msg void	OnAssetBrowserClearViewportFilter();
		afx_msg void	OnAssetBrowserAutoChangeViewportSelection();
		afx_msg void	OnAssetBrowserAutoFilterFromViewportSelection();
		afx_msg void	OnAssetBrowserConsoleHotUpdateFromSelection();
		afx_msg void	OnVisualBudgetUpdate();
		afx_msg void	OnVisualBudgetAnalyzeSingleSector();
		afx_msg void	OnVisualBudgetDisplaySectors();
		afx_msg void	OnVisualBudgetChangeCellSize( NMHDR* pNMHDR, LRESULT* pResult );
		afx_msg void	OnUpdateVisualBudgetAnalyzeSingleSector( CCmdUI* pUI );
		afx_msg void	OnUpdateVisualBudgetDisplaySectors( CCmdUI* pUI );
		afx_msg void	OnUpdateVisualBudgetElement( CCmdUI* pUI );
		afx_msg void	OnVisualBudgetCheckElement( UINT id );
		afx_msg void	OnUpdateVisualBudgetDisplayMode( CCmdUI* pUI );
		afx_msg void	OnVisualBudgetCheckDisplayMode( UINT id );
		afx_msg void	OnUpdateVisualBudgetResourceType( CCmdUI* pUI );
		afx_msg void	OnVisualBudgetCheckResourceType( UINT id );

		CXTPDockingPane*												m_pDockPaneListView;
		CXTPDockingPane*												m_pDockPanePreview;
		CXTPStatusBar														m_oWindowStatusBar;
		CXTPRibbonBar*													m_pRibbonBar;
		CDynamicRibbonBar												m_dynamicRibbonBarLoader;
		CXTSplitterWnd													m_oThumbsSplitter;
		CAssetBrowserReportControl							m_oAssetListView;
		CAssetBrowserSearchDlg									m_searchDlg;
		class CAssetListReportHeader*						m_pReportHeader;
		CAssetViewer														m_oAssetViewer;
		CAssetBrowserPreviewDlg									m_assetPreviewDlg;
		CVisualBudgetSystemSettings							m_oVisualBudgetSystem;
		IAssetDisplayDatabase*									m_poCurrentAssetDatabase;
		TAssetDatabases													m_cAssetDatabases;
		BOOL																		m_checkedVisibleDatabases[AssetBrowser::kMaxDatabaseCount];
		int																			m_nonThreadedAssetsCrtCount;
		static CAssetBrowserDialog*							s_poCurrentInstance;
		static bool															s_bInitialized;
		class CAssetListVirtualRecord*					m_pAssetVirtualRecord;
		IAssetDisplayDatabase::TAssetFields			m_commonFields, m_allFields;
		CXTPControlComboBox*										m_pCbThumbSize;
		CXTPControlComboBox*										m_pCbFilterPresets;
		CXTPControlCheckBox*										m_pChkUsedInLevel;
		CXTPControlCheckBox*										m_pChkLoadedInLevel;
		CXTPControlCheckBox*										m_pChkShowFavourites;
		CXTPControlCheckBox*										m_pChkHideLods;
		CXTPControlComboBox*										m_pCbVbsCellSize;
		CAssetBrowserFilterPresetsDlg::TPresetNameAndPresetMap m_filterPresets;
		// if this is true, then the list view will show the merged fields, all fields from the current selected databases,
		// if its false, then it will show only the common fields of the selected databases
		bool																		m_bShowMergedDatabaseFields;
		bool																		m_bAutoChangeViewportSelection;
		bool																		m_bAutoFilterFromViewportSelection;
		// guardian bool, to avoid ping-pong selection changed events between listview and thumbsview (this control) when selecting assets
		bool																		m_bSelectAssetsFromListView;

		BOOL									m_bVbsEnabledStatus;
		BOOL									m_bVbsAnalyzeSingleSector;
		int										m_vbsDisplayType;
		int										m_vbsResourceType;
		int										m_vbsCellSizeIndex;
		BOOL									m_bVbsAccountTerrain;
		BOOL									m_bVbsAccountMiscelaneous;
		BOOL									m_bVbsAccountBrushes;
		BOOL									m_bVbsAccountVegetation;
		BOOL									m_bVbsAccountRoads;
		BOOL									m_bVbsAccountClouds;
		BOOL									m_bVbsAccountEntities;
		BOOL									m_bVbsAccountDecals;
		CVisualBudgetSystem&	m_roVisualBudgetSystem;
};

#endif //__AssetBrowser_h__
